Creating our first Server with NODE JS
For more, read the Started documentation
For that, we follow the next steps:
This utility will walk you through creating a package.json file. It only covers the most common items, and tries to guess sensible defaults. See `npm help init` for definitive documentation on these fields and exactly what they do. Use `npm install <pkg>` afterwards to install a package and save it as a dependency in the package.json file. Press ^C at any time to quit. package name: (nodeserver) version: (1.0.0) description: entry point: (server.js) test command: git repository: keywords: author: license: (ISC) About to write to /home/rychy/Documents/STUDY time/2022 & 2023 Practice/webProgress/Session 24 Express/nodeServer/package.json: { "name": "nodeserver", "version": "1.0.0", "description": "", "main": "server.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC" } Is this OK? (yes) ╭───────────────────────────────────────────────────────────────╮ │ │ New major version of npm available! 6.14.18 → 9.6.6 │ Changelog: https://github.com/npm/cli/releases/tag/v9.6.6 │ Run npm install -g npm to update! │ ╰───────────────────────────────────────────────────────────────╯rychy@popped:~/Documents/webProgress/Session 24 Express/node$npm install express rychy@popped:~/Documents/webProgress/Session 24 Express/node$node server.js
Example app listening on port 3000
The code for the first hello world, is:
const express = require('express');
const app = express();
const port = 3000;
Get is a method for hadle the request from the browser, and the response is for respond or resolv the requested
app.get('/', (req, res) => {
res.send("<h1>Hello World!</h1><h1>I'm Rychy</h1>");
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});